/**
 * 
 */
package com.orcwar.engine.units;

import com.orcwar.engine.OWMap;
import com.orcwar.engine.OWPlayer;
import com.orcwar.engine.OWTile;
import com.orcwar.engine.OWUnit;
import com.orcwar.engine.tile.OWAttackTile;
import com.orcwar.enums.OWAttackTileTypeEnum;
import com.orcwar.enums.SpriteEnum;

/**
 * @author Callil
 *
 */
public class OWHommeLance extends OWUnit {

	/**
	 * Constructor
	 * @param id the id
	 */
	public OWHommeLance(int id, OWPlayer player) {
		super(id, player, SpriteEnum.UNIT_HOMMELANCE);
		
		//The hommelance can attack 2 times
		setAttackPoints(1);
		setMaxAttackPoints(1);
		//and move up to 3 tiles
		setMaxMovePoints(3);
		setMovePoints(3);
	}
	
	@Override
	public OWAttackTile[] getGlobalMandatoryAvailableTiles(OWMap map, OWTile fromTile) {
		return null;
	}
	
	@Override
	public boolean isRangeOk(int srcX, int srcY, int targetX, int targetY) {
		//The hommelance can attack all around him at 2 tiles horizontally and
		//vertically, and at 1 tile on diagonals
		// _ _ _ _ _
		//| | |x| | |
		//| |x|x|x| |
		//|x|x|H|x|x|
		//| |x|x|x| |
		//|_|_|x|_|_|
		
		int xDistance = Math.abs(srcX - targetX);
		int yDistance =  Math.abs(srcY - targetY);
		
		boolean touchHorizontal = yDistance == 0 && (xDistance == 1 || xDistance == 2);
		boolean touchVertical = xDistance == 0 && (yDistance == 1 || yDistance == 2);
		boolean touchDiagonal = xDistance == 1 && yDistance == 1;
		
		return touchHorizontal || touchVertical || touchDiagonal;
	}

	@Override
	public OWAttackTile[][] getAttackTiles(OWMap map, OWTile fromTile) {
		//The hommelance can attack all around him at 2 tiles horizontally and
		//vertically, and at 1 tile on diagonals
		// _ _ _ _ _
		//| | |x| | |
		//| |x|x|x| |
		//|x|x|H|x|x|
		//| |x|x|x| |
		//|_|_|x|_|_|
		int x;
		int y;
		if (fromTile != null) {
			x = fromTile.getX();
			y = fromTile.getY();
		} else {
			x= this.getTile().getX();
			y = this.getTile().getY();
		}
		OWAttackTile[][] attackTiles = new OWAttackTile[][]{ 
				new OWAttackTile[] {OWAttackTile.create(map.getTile(x-1, y-1), OWAttackTileTypeEnum.ATTACKABLE)},
				
				new OWAttackTile[] {OWAttackTile.create(map.getTile(x-1, y), OWAttackTileTypeEnum.ATTACKABLE),
						OWAttackTile.create(map.getTile(x-2, y), OWAttackTile.create(map.getTile(x-1, y), OWAttackTileTypeEnum.ATTACKABLE))}, 
				
				new OWAttackTile[] {OWAttackTile.create(map.getTile(x-1, y+1), OWAttackTileTypeEnum.ATTACKABLE)},
				
				new OWAttackTile[] {OWAttackTile.create(map.getTile(x, y-1), OWAttackTileTypeEnum.ATTACKABLE),
						OWAttackTile.create(map.getTile(x, y-2), OWAttackTile.create(map.getTile(x, y-1), OWAttackTileTypeEnum.ATTACKABLE))}, 
				
				new OWAttackTile[] {OWAttackTile.create(map.getTile(x, y+1), OWAttackTileTypeEnum.ATTACKABLE),
						OWAttackTile.create(map.getTile(x, y+2), OWAttackTile.create(map.getTile(x, y+1), OWAttackTileTypeEnum.ATTACKABLE))},
				
				new OWAttackTile[] {OWAttackTile.create(map.getTile(x+1, y-1), OWAttackTileTypeEnum.ATTACKABLE)},
				
				new OWAttackTile[] {OWAttackTile.create(map.getTile(x+1, y), OWAttackTileTypeEnum.ATTACKABLE),
						OWAttackTile.create(map.getTile(x+2, y), OWAttackTile.create(map.getTile(x+1, y), OWAttackTileTypeEnum.ATTACKABLE))},
				
				new OWAttackTile[] {OWAttackTile.create(map.getTile(x+1, y+1), OWAttackTileTypeEnum.ATTACKABLE)}};

		return attackTiles;
	}

}
